/*
 * MenuBean.java
 * 
 * Copyright (c) 2011 Administrator. 
 * 
 * This file is part of RMS.
 * 
 * RMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RMS.  If not, see <http ://www.gnu.org/licenses/>.
 */
package com.itrustsolution.rms.managedbean;

import com.icesoft.faces.component.tree.IceUserObject;
import com.itrustsolution.rms.util.UrlNodeUserObject;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

/**
 * <p/>
 * A basic backing bean for a ice:tree component.  The only instance variable
 * needed is a DefaultTreeModel Object which is bound to the icefaces tree
 * component in the jspx code.</p>
 * <p/>
 * The tree created by this backing bean is used to control the selected
 * panel in a ice:panelStack.
 * </p>
 * @author Administrator
 */
public class MenuBean {
    
    // tree default model, used as a value for the tree component
    private DefaultTreeModel model;

    // default node icons for xp thme
    private static final String XP_BRANCH_CONTRACTED_ICON = "./xmlhttp/css/xp/css-images/tree_folder_open.gif";
    private static final String XP_BRANCH_EXPANDED_ICON = "./xmlhttp/css/xp/css-images/tree_folder_close.gif";
    private static final String XP_BRANCH_LEAF_ICON = "./xmlhttp/css/xp/css-images/tree_document.gif";
    
    /** Creates a new instance of MenuBean */
    public MenuBean() {
        if(model != null) return;
        else {
//        // create root node with its children expanded
//        DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode();
//        IceUserObject rootObject = new IceUserObject(rootTreeNode);
//        rootObject.setText("Root Node");
//        rootObject.setExpanded(true);
//        rootObject.setBranchContractedIcon(XP_BRANCH_CONTRACTED_ICON);
//        rootObject.setBranchExpandedIcon(XP_BRANCH_EXPANDED_ICON);
//        rootObject.setLeafIcon(XP_BRANCH_LEAF_ICON);
//        rootTreeNode.setUserObject(rootObject);
//
//        // model is accessed by by the ice:tree component
//        model = new DefaultTreeModel(rootTreeNode);
//
//        // add some child notes
//        for (int i = 0; i < 3; i++) {
//            DefaultMutableTreeNode branchNode = new DefaultMutableTreeNode();
//            IceUserObject branchObject = new IceUserObject(branchNode);
//            branchObject.setText("node-" + i);
//            branchObject.setBranchContractedIcon(XP_BRANCH_CONTRACTED_ICON);
//            branchObject.setBranchExpandedIcon(XP_BRANCH_EXPANDED_ICON);
//            branchObject.setLeafIcon(XP_BRANCH_LEAF_ICON);
//            branchObject.setExpanded(true);
//            branchObject.setLeaf(true);
//            branchNode.setUserObject(branchObject);
//            rootTreeNode.add(branchNode);
//            // add some more sub children
//            for (int k = 0; k < 2; k++) {
//                System.out.println("k = " + k);
//                DefaultMutableTreeNode subBranchNode = new DefaultMutableTreeNode();
//                IceUserObject subBranchObject = new IceUserObject(subBranchNode);
//                subBranchObject.setText("sub-node-" + i + "-" + k);
//                subBranchObject.setBranchContractedIcon(XP_BRANCH_CONTRACTED_ICON);
//                subBranchObject.setBranchExpandedIcon(XP_BRANCH_EXPANDED_ICON);
//                subBranchObject.setLeafIcon(XP_BRANCH_LEAF_ICON);
//                branchObject.setLeaf(true);
//                subBranchNode.setUserObject(subBranchObject);
//                branchNode.add(subBranchNode);
//            }
//        }
        
        // create root node with its children expanded
        DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode();
        UrlNodeUserObject rootObject = new UrlNodeUserObject(rootTreeNode);
        rootObject.setText("RMS Application");
        rootObject.setUrl("main.jsf");
        rootObject.setExpanded(true);
        rootObject.setDisplayPanel("splash");
        rootObject.setBranchContractedIcon(XP_BRANCH_CONTRACTED_ICON);
        rootObject.setBranchExpandedIcon(XP_BRANCH_EXPANDED_ICON);
        rootObject.setLeafIcon(XP_BRANCH_LEAF_ICON);
        rootTreeNode.setUserObject(rootObject);

        // model is accessed by by the ice:tree component
        model = new DefaultTreeModel(rootTreeNode);

        // add Tag parent node
        DefaultMutableTreeNode parentNode = new DefaultMutableTreeNode();
        UrlNodeUserObject parentObject = new UrlNodeUserObject(parentNode);
        parentObject.setText("Dashboard");
        parentObject.setUrl("home.iface");
        parentObject.setExpanded(true);
        parentObject.setBranchContractedIcon(XP_BRANCH_CONTRACTED_ICON);
        parentObject.setBranchExpandedIcon(XP_BRANCH_EXPANDED_ICON);
        parentObject.setLeafIcon(XP_BRANCH_LEAF_ICON);
        parentNode.setUserObject(parentObject);
        rootTreeNode.add(parentNode);
        
        // add ui:component child node
        DefaultMutableTreeNode branchNode = new DefaultMutableTreeNode();
        UrlNodeUserObject branchObject = new UrlNodeUserObject(branchNode);
        branchObject.setText("ui:component");
        branchObject.setUrl("component.iface");
        branchObject.setLeaf(true);
        branchObject.setBranchContractedIcon(XP_BRANCH_CONTRACTED_ICON);
        branchObject.setBranchExpandedIcon(XP_BRANCH_EXPANDED_ICON);
        branchObject.setLeafIcon(XP_BRANCH_LEAF_ICON);
        branchNode.setUserObject(branchObject);
        parentNode.add(branchNode);

        //add ui:composition child node
        branchNode = new DefaultMutableTreeNode();
        branchObject = new UrlNodeUserObject(branchNode);
        branchObject.setText("ui:composition");
        branchObject.setUrl("composition.iface");
        branchObject.setLeaf(true);
        branchObject.setBranchContractedIcon(XP_BRANCH_CONTRACTED_ICON);
        branchObject.setBranchExpandedIcon(XP_BRANCH_EXPANDED_ICON);
        branchObject.setLeafIcon(XP_BRANCH_LEAF_ICON);
        branchNode.setUserObject(branchObject);
        parentNode.add(branchNode);
        
        //add ui:debug child node
        branchNode = new DefaultMutableTreeNode();
        branchObject = new UrlNodeUserObject(branchNode);
        branchObject.setText("ui:debug");
        branchObject.setUrl("debug.iface");
        branchObject.setLeaf(true);
        branchObject.setBranchContractedIcon(XP_BRANCH_CONTRACTED_ICON);
        branchObject.setBranchExpandedIcon(XP_BRANCH_EXPANDED_ICON);
        branchObject.setLeafIcon(XP_BRANCH_LEAF_ICON);
        branchNode.setUserObject(branchObject);
        parentNode.add(branchNode);
        

        /*-------------------------------------- MASTER --------------------------------------*/
        parentNode = new DefaultMutableTreeNode();
        parentObject = new UrlNodeUserObject(parentNode);
        parentObject.setText("Master");
        parentObject.setUrl("example.iface");
        parentObject.setExpanded(true);
        parentObject.setBranchContractedIcon(XP_BRANCH_CONTRACTED_ICON);
        parentObject.setBranchExpandedIcon(XP_BRANCH_EXPANDED_ICON);
        parentObject.setLeafIcon(XP_BRANCH_LEAF_ICON);
        parentNode.setUserObject(parentObject);
        rootTreeNode.add(parentNode);
        
        //add ui:repeat child node
        branchNode = new DefaultMutableTreeNode();
        branchObject = new UrlNodeUserObject(branchNode);
        branchObject.setText("User");
        branchObject.setUrl("repeat.iface");
        branchObject.setLeaf(true);
        branchObject.setBranchContractedIcon(XP_BRANCH_CONTRACTED_ICON);
        branchObject.setBranchExpandedIcon(XP_BRANCH_EXPANDED_ICON);
        branchObject.setLeafIcon(XP_BRANCH_LEAF_ICON);
        branchNode.setUserObject(branchObject);
        parentNode.add(branchNode);
        
        //add ui:repeat child node
        branchNode = new DefaultMutableTreeNode();
        branchObject = new UrlNodeUserObject(branchNode);
        branchObject.setText("Client");
        branchObject.setUrl("http://localhost:8080/rms/master/client.jsf");
        branchObject.setLeaf(true);
        branchObject.setBranchContractedIcon(XP_BRANCH_CONTRACTED_ICON);
        branchObject.setBranchExpandedIcon(XP_BRANCH_EXPANDED_ICON);
        branchObject.setLeafIcon(XP_BRANCH_LEAF_ICON);
        branchNode.setUserObject(branchObject);
        parentNode.add(branchNode);
        
        //add ui:repeat child node
        branchNode = new DefaultMutableTreeNode();
        branchObject = new UrlNodeUserObject(branchNode);
        branchObject.setText("Product");
        branchObject.setUrl("repeat.iface");
        branchObject.setLeaf(true);
        branchObject.setBranchContractedIcon(XP_BRANCH_CONTRACTED_ICON);
        branchObject.setBranchExpandedIcon(XP_BRANCH_EXPANDED_ICON);
        branchObject.setLeafIcon(XP_BRANCH_LEAF_ICON);
        branchNode.setUserObject(branchObject);
        parentNode.add(branchNode);
        
        //add ui:repeat child node
        branchNode = new DefaultMutableTreeNode();
        branchObject = new UrlNodeUserObject(branchNode);
        branchObject.setText("Department");
        branchObject.setUrl("repeat.iface");
        branchObject.setLeaf(true);
        branchObject.setBranchContractedIcon(XP_BRANCH_CONTRACTED_ICON);
        branchObject.setBranchExpandedIcon(XP_BRANCH_EXPANDED_ICON);
        branchObject.setLeafIcon(XP_BRANCH_LEAF_ICON);
        branchNode.setUserObject(branchObject);
        parentNode.add(branchNode);
        
        
        /*-------------------------------------- TICKET --------------------------------------*/
        parentNode = new DefaultMutableTreeNode();
        parentObject = new UrlNodeUserObject(parentNode);
        parentObject.setText("Ticket");
        parentObject.setUrl("example.iface");
        parentObject.setExpanded(true);
        parentObject.setBranchContractedIcon(XP_BRANCH_CONTRACTED_ICON);
        parentObject.setBranchExpandedIcon(XP_BRANCH_EXPANDED_ICON);
        parentObject.setLeafIcon(XP_BRANCH_LEAF_ICON);
        parentNode.setUserObject(parentObject);
        rootTreeNode.add(parentNode);
        
        
        /*-------------------------------------- REPORT --------------------------------------*/
        parentNode = new DefaultMutableTreeNode();
        parentObject = new UrlNodeUserObject(parentNode);
        parentObject.setText("Report");
        parentObject.setUrl("example.iface");
        parentObject.setExpanded(true);
        parentObject.setBranchContractedIcon(XP_BRANCH_CONTRACTED_ICON);
        parentObject.setBranchExpandedIcon(XP_BRANCH_EXPANDED_ICON);
        parentObject.setLeafIcon(XP_BRANCH_LEAF_ICON);
        parentNode.setUserObject(parentObject);
        rootTreeNode.add(parentNode);
        
        
        /*-------------------------------------- LOG --------------------------------------*/
        parentNode = new DefaultMutableTreeNode();
        parentObject = new UrlNodeUserObject(parentNode);
        parentObject.setText("Log");
        parentObject.setUrl("example.iface");
        parentObject.setExpanded(true);
        parentObject.setBranchContractedIcon(XP_BRANCH_CONTRACTED_ICON);
        parentObject.setBranchExpandedIcon(XP_BRANCH_EXPANDED_ICON);
        parentObject.setLeafIcon(XP_BRANCH_LEAF_ICON);
        parentNode.setUserObject(parentObject);
        rootTreeNode.add(parentNode);
        }
    }
    
    /**
     * Gets the tree's default model.
     *
     * @return tree model.
     */
    public DefaultTreeModel getModel() {
        return model;
    }

}
